package com.limingru.newbee.api;

import com.limingru.newbee.api.param.OrderDetailParam;
import com.limingru.newbee.api.param.OrderRemoveParam;
import com.limingru.newbee.api.param.OrderSaveParam;
import com.limingru.newbee.api.vo.CartItemVO;
import com.limingru.newbee.common.NewBeeMallException;
import com.limingru.newbee.common.ServiceResultEnum;
import com.limingru.newbee.config.annotation.TokenToUser;
import com.limingru.newbee.entity.Address;
import com.limingru.newbee.entity.Order;
import com.limingru.newbee.entity.User;
import com.limingru.newbee.service.CartService;
import com.limingru.newbee.service.OrderService;
import com.limingru.newbee.service.UserAddressService;
import com.limingru.newbee.util.Result;
import com.limingru.newbee.util.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class OrderApi {

    @Resource
    OrderService orderService;

    @Resource
    CartService cartService;

    @Resource
    UserAddressService userAddressService;

    @PostMapping("/order/save")
    @ApiOperation(value = "生成订单接口", notes = "入参为购物车id数组和地址id")
    public Result<String> saveOrder(@RequestBody OrderSaveParam orderSaveParam, @TokenToUser User user) {
        int totalPrice = 0;
        if (orderSaveParam.getCartItemsIds() == null || orderSaveParam.getAddressId() == null || orderSaveParam.getCartItemsIds().length < 1) {
            NewBeeMallException.fail(ServiceResultEnum.PARAM_ERROR.getText());
        }
        List<CartItemVO> cartItemsForSettle = cartService.getCartItemsForSettle(Arrays.asList(orderSaveParam.getCartItemsIds()), user.getUserId());
        if (CollectionUtils.isEmpty(cartItemsForSettle)) {
            NewBeeMallException.fail("参数异常");
        } else {
            for (CartItemVO c : cartItemsForSettle) {
                totalPrice += c.getSellingPrice() * c.getGoodsCount();
            }
            if (totalPrice < 1) {
                NewBeeMallException.fail("价格异常");
            }
        }

        Address address = userAddressService.getAddressByAddressId(orderSaveParam.getAddressId());
        if (address.getUserId() != user.getUserId()) {
            NewBeeMallException.fail(ServiceResultEnum.FORBID.getText());
        }

        String orderNo = orderService.saveOrder(cartItemsForSettle, address, user);
        if (!ServiceResultEnum.DB_ERROR.getText().equals(orderNo)) {
            return ResultGenerator.generateSuccessResult((Object) orderNo);
        }
        return ResultGenerator.generateFailResult(orderNo);
    }

    @GetMapping("/order/list")
    @ApiOperation(value = "订单列表", notes = "查看用户订单列表")
    public Result<List<Order>> orderList(@TokenToUser User user) {
        List<Order> orderList = orderService.orderList(user.getUserId());
        return ResultGenerator.generateSuccessResult(orderList);
    }

    @PostMapping("/order/detail")
    @ApiOperation(value = "订单详情", notes = "传参为token和orderId")
    public Result<Order> orderDetail(@RequestBody OrderDetailParam orderDetailParam, @TokenToUser User user) {
        Order order = orderService.orderDetail(Arrays.asList(orderDetailParam.getOrderId()));
        if (order != null && order.getUserId() != user.getUserId()) {
            return ResultGenerator.generateFailResult(ServiceResultEnum.FORBID.getText());
        }
        return ResultGenerator.generateSuccessResult(order);
    }

    @PostMapping("/order/delete")
    public Result<String> removeOrder(@RequestBody OrderRemoveParam orderRemoveParam, @TokenToUser User user) {
        Order order = new Order();
        BeanUtils.copyProperties(orderRemoveParam, order);
        order.setUserId(user.getUserId());
        String removeResult = orderService.deleteOrder(order);
        if (ServiceResultEnum.SUCCESS.getText().equals(removeResult)) {
            return ResultGenerator.generateSuccessResult((Object) removeResult);
        }
        return ResultGenerator.generateFailResult(removeResult);
    }
}
